package device.itself.util;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.jersey.internal.util.ReflectionHelper;

import device.itself.annotaion.ContextType;
import device.itself.annotaion.ContextField;
import device.itself.annotaion.SearchEngine;
import device.itself.annotaion.SearchLanguage;

public class AnnotationReflectionUtil {

	private static final Logger LOGGER = LogManager.getRootLogger();

	private AnnotationReflectionUtil()
	{
	}

	public static String getAnnotationType(Object obj)
	{
		String KeyValue="";
		Class<? extends Object> classObject=obj.getClass();
		if(classObject.isAnnotationPresent(ContextType.class))
		{
			Annotation annotation=classObject.getAnnotation(ContextType.class);
			ContextType contextVariable=(ContextType)annotation;
			KeyValue=contextVariable.key()+contextVariable.Value();
		}
		
		return KeyValue;
	}
	
	public static Map<String, String> getAnnotationFieldsPA(Class<?> classObject,Object obj)
	{
		Map<String, String> fieldMap=new HashMap<String, String>();
		Field[] fields=AccessController.doPrivileged(ReflectionHelper.getAllFieldsPA(classObject));
		try {
			for(Field lfield:fields)
			{
				lfield.setAccessible(true);
				if(lfield.isAnnotationPresent(ContextField.class))
				{
					Annotation annotation=lfield.getAnnotation(ContextField.class);
					ContextField contextVariable=(ContextField)annotation;
					try {
						fieldMap.put(contextVariable.key().toString(), lfield.get(obj).toString());
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return fieldMap;
	}
	public static Map<String, String> getAnnotationFields(Object obj)
	{
		Map<String, String> fieldMap=new HashMap<String, String>();
		Field[] fields=obj.getClass().getFields();
		try {
			for(Field lfield:fields)
			{
				lfield.setAccessible(true);
				if(lfield.isAnnotationPresent(ContextField.class))
				{
					Annotation annotation=lfield.getAnnotation(ContextField.class);
					ContextField contextVariable=(ContextField)annotation;
					LOGGER.debug("Key:"+contextVariable.key()+", Value:"+lfield.get(obj));
					fieldMap.put(contextVariable.key().toString(), lfield.get(obj).toString());
				}
				else if(lfield.isAnnotationPresent(SearchEngine.class))
				{
					LOGGER.debug("Value:"+lfield.get(obj));
					fieldMap.put("SearchEngine", lfield.get(obj).toString());
				}
				else if(lfield.isAnnotationPresent(SearchLanguage.class))
				{
					LOGGER.debug("Value:"+lfield.get(obj));
					fieldMap.put("SearchLanguage", lfield.get(obj).toString());
				}
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return fieldMap;
	}
	public static JSONObject getAnnotationJSON(Object obj)
	{
		JSONObject jsTitle=new JSONObject();
		JSONObject rtnObject=new JSONObject();
		jsTitle.put(getAnnotationType(obj), getAnnotationFields(obj));
		rtnObject.putAll(getAnnotationFields(obj));
		
		return jsTitle;
	}
}
